//01lodash中的组合函数
const _ = require("lodash");
// flow 和 flowRight 两个组合函数
// const fn = _.flowRight(_.toUpper, _.first, _.reverse)
//不管怎么组合最后的结果一致
const fn = _.flowRight(_.flowRight(_.toUpper, _.first), _.reverse);
console.log(fn(["one", "two", "three"]));

//02手写flowRight
// function compose(...args) {
//     return function (v) {
//         return args.reverse().reduce((acc, fn) => {
//             return fn(acc)
//         }, v)
//     }
// }
const reverse = (arr) => arr.reverse();
const first = (arr) => arr[0];
const toUpper = (s) => s.toUpperCase();
const compose = (...args) => (v) =>
  args.reverse().reduce((acc, fn) => fn(acc), v);
let fn2 = compose(toUpper, first, reverse);
console.log(fn2(["one", "two", "three"]));

// 03组合函数调试  Never Say Die=> never-say-die
// const log = v => {
//     console.log(v)
//     return v
// }
const log = _.curry((tag, v) => {
  console.log(tag, v);
  return v;
});
const map = _.curry((fn, arr) => _.map(arr, fn));
const split = _.curry((sep, str) => _.split(str, sep));
const join = _.curry((sep, str) => _.join(str, sep));
const fn3 = _.flowRight(join("-"), log("map之后"), map(_.toLower), split(" "));
console.log(fn3("Never Say Die"));

// 04  lodash 的fp模块 改写上面代码
const fp = require("lodash/fp");
const fn4 = _.flowRight(
  fp.join("-"),
  log("map之后"),
  fp.map(fp.toLower),
  fp.split(" ")
);
console.log(fn4("Never Say Die"));

//05map方法的区别
/*
 数据在先，方法在后
*/
console.log(_.map(["23", "8", "10"], parseInt));
// parseInt('23',0,array) 23
// parseInt('8',1,array)  NaN
// parseInt('10',2,array) 2
/*
  方法在前，数据在后
*/
console.log(fp.map(parseInt, ["23", "8", "10"]));
